package cn.edu.zafu.bigdata.xct.wlbc;

import java.io.*;
import java.net.*;

public class TCPClient {
    public static void main(String[] args) {
        String host = "192.168.16.54"; // 替换为你同学的服务端IP地址
        int port = 1234; // 端口号，需要和服务端一致

        try (Socket socket = new Socket(host, port)) {
            System.out.println("连接到服务端");

            // 创建输入输出流
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

            String userInput;
            System.out.println("请输入消息：");
            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput); // 发送消息到服务端

                // 读取服务端的回复
                String fromServer = in.readLine();
                System.out.println("服务端回复：" + fromServer);

                if ("exit".equalsIgnoreCase(userInput)) {
                    break;
                }
            }
        } catch (UnknownHostException e) {
            System.err.println("不知道主机: " + host);
        } catch (IOException e) {
            System.err.println("无法获取I/O为该连接: " + host);
        }
    }
}